# 文件上传组件
from langchain_community.document_loaders import UnstructuredFileLoader
import os
import tempfile
import re


def read_uploaded_file(file_bytes, file_name):
    """读取上传的文件并返回内容"""
    suffix = os.path.splitext(file_name)[1]
    with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp_file:
        tmp_file.write(file_bytes)
        tmp_path = tmp_file.name

    try:
        loader = UnstructuredFileLoader(tmp_path)
        documents = loader.load()
        return clean_text(documents[0].page_content)
    except Exception as e:
        raise Exception(f"解析文档失败：{e}")
    finally:
        os.remove(tmp_path)


# 文件清洗
def clean_text(text):
    """清理文档文本，移除不必要的行号和其他噪音"""
    # 1. 移除单独的数字行（行号）
    cleaned = re.sub(r'^\d+\s*$', '', text, flags=re.MULTILINE)

    # 2. 移除行首的数字+空格模式（行内行号）
    cleaned = re.sub(r'^\d+\s+', '', cleaned, flags=re.MULTILINE)

    # 3. 合并连续的JSON结构
    cleaned = re.sub(r'"code":\s*\d+,', '"code": 0,', cleaned)

    # 4. 优化JSON格式显示
    cleaned = re.sub(r'{\s*"', '{\n    "', cleaned)
    cleaned = re.sub(r',\s*"', ',\n    "', cleaned)
    cleaned = re.sub(r'}\s*$', '}\n', cleaned, flags=re.MULTILINE)

    # 5. 移除多余的空白行
    cleaned = re.sub(r'\n\s*\n', '\n\n', cleaned)

    return cleaned.strip()



if __name__ == '__main__':
    loader = UnstructuredFileLoader(r'D:\007\debug\apidemokeywordspyts\temp_uploads\王者aipass问答.docx')
    documents = loader.load()

    # # 可选：分块处理
    # splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
    # texts = splitter.split_documents(documents)
    # 2. 直接使用文档内容
    document_content = clean_text(documents[0].page_content)
    print(document_content)